<?php
require_once 'Xml.php';

class Models_Controller
{
	protected $_objXml;
	protected $_SourceName;
	protected $_Data;
	protected $_bLoadComplete;

	public function loadFromXml( $aryForceEnum=array() )
	{
		$this->_bLoadComplete = TRUE;
		$this->_objXml = new Lib_Xml();
		$this->_Data = &$this->_objXml->loadFromFile($this->_SourceName, $aryForceEnum );
		if ( $this->_Data === FALSE ) {
			$this->_bLoadComplete = FALSE;
		}
		return $this->_Data;
	}

	public function saveToXml()
	{
		if ( $this->_bLoadComplete == FALSE ) {
			$this->_objXml->aryXml = $this->_Data;
		}
		$this->_objXml->saveToFile( $this->_SourceName );
	}

	public function createId()
	{
		$aryDivide = explode( ' ', microtime() );
		$iResult = $aryDivide[0] + $aryDivide[1];
		return $iResult;
	}

	public function createInclimentId($sFileName)
	{
		if ( !file_exists($sFileName) ) {
			file_put_contents($sFileName, 1000);
			return 1000;
		} else {
			$iResult = file_get_contents($sFileName);
			$iResult ++;
			file_put_contents($sFileName, $iResult);
			return $iResult;
		}
	}

	public function getData()
	{
		return $this->_Data;
	}

	public function unlinkXml()
	{
		unlink($this->_SourceName);
	}
}

